package com.dinadi.yidi.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dinadi.yidi.R;
import com.dinadi.yidi.bean.MyActivitysRequestEntity;
import com.dinadi.yidi.ui.activity.OrderInformationActivity;
import com.dinadi.yidi.utils.FormatCurrentData;
import com.dinadi.yidi.utils.ImageUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/6/29
 * Time: 10:59
 */

public class MyActivitysAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context mContext;
    private List<MyActivitysRequestEntity> mDatas = new ArrayList<>();
    private String type, productId;

    public void addAllData(List<MyActivitysRequestEntity> dataList) {
        this.mDatas.addAll(dataList);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.mDatas.clear();
    }

    public MyActivitysAdapter(Context context) {
        mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_my_activitys_item, parent, false);
        return new MyActivitysAdapter.ItemViewHolder(v);

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        String startTime = mDatas.get(position).getStartTime();
        //String startDate = DateFormat.getDateInstance(DateFormat.FULL).format(FormatCurrentData.strToDateLong(startTime));
        String endTime = mDatas.get(position).getEndTime();
        //  String endDate = DateFormat.getDateInstance(DateFormat.FULL).format(FormatCurrentData.strToDateLong(endTime));
      //  String endDate = DateFormat.getDateInstance(DateFormat.FULL).format(FormatCurrentData.strToDateLong(endTime));
        String startDate = FormatCurrentData.getDate(startTime);
        String endDate = FormatCurrentData.getDate(endTime);
        itemViewHolder.activitys_time.setText(startDate + " 到 " + endDate);
        itemViewHolder.activitys_title.setText(mDatas.get(position).getTitle());
        // itemViewHolder.activitys_time.setText(startDate.substring(0, startDate.length() - 3) + "-" + endDate.substring(0, endDate.length() - 3));
       // itemViewHolder.activitys_time.setText(startDate.substring(0, startDate.length() - 3) + "-" + endDate.substring(0, endDate.length() - 3));
        itemViewHolder.activitys_place.setText(mDatas.get(position).getAddress());
        String signPrice = mDatas.get(position).getSignPrice();
        if (signPrice.equals("0")) {
            itemViewHolder.activitys_cost.setText("免费");
        } else {
            itemViewHolder.activitys_cost.setText(signPrice);
        }

        String imgUrl = mDatas.get(position).getCoverImg();
        if (imgUrl != null) {
            if (!imgUrl.equals("")) {
                ImageUtils.loadImageView(mContext, imgUrl, itemViewHolder.imageView);
            } else {
                ImageUtils.loadImageView(mContext, R.drawable.me_img_default, itemViewHolder.imageView);
            }
        } else {
            ImageUtils.loadImageView(mContext, R.drawable.symbols_bitmap, itemViewHolder.imageView);

        }
        String verificationStatus = mDatas.get(position).getVerificationStatus();
        String signStatus = mDatas.get(position).getSignStatus();
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(date);
/*两个Date类型的变量可以通过compareTo方法来比较。此方法的描述是这样的：如果参数 Date 等于此 Date，则返回值 0；如果此 Date 在 Date 参数之前，则返回小于 0 的值；如果此 Date 在 Date 参数之后，则返回大于 0 的值。
实际上比较的是自1970 年 1 月 1 日 00:00:00 GMT以来的毫秒数。毫秒数越大的时间越大。*/
        String DateStr1 = time;
        String DateStr2 = startTime;
        String DateStr3 = endTime;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateTime1 = null;
        try {
            dateTime1 = dateFormat.parse(DateStr1);
            Date dateTime2 = dateFormat.parse(DateStr2);
            Date dateTime3 = dateFormat.parse(DateStr3);
            int i = dateTime1.compareTo(dateTime2);
            int j = dateTime1.compareTo(dateTime3);

            if (j == 0) {
                        /*两个时间一样*/
            } else if (j < 0) {
                        /*活动没有结束*/
                if (i == 0) {
                        /*两个时间一样*/

                } else if (i < 0) {
//signStatus   2：已报名 3：已取消",
                    if (signStatus.equals("2")) {
                        if (verificationStatus.equals("1")) {
                            // 未核销
                            itemViewHolder.mengban.setVisibility(View.GONE);

                        } else {
                            itemViewHolder.mengban.setVisibility(View.VISIBLE);
                        }

//                        if (verificationStatus == 2) 以核销
                    } else {
                        // 已取消
                        itemViewHolder.mengban.setVisibility(View.VISIBLE);
                    }

/*// verificationStatus1-未核销 2-已核销
                        *//*活动还没有开始*//*
                    if (verificationStatus.equals("2")) {
                        itemViewHolder.mengban.setVisibility(View.VISIBLE);
                    } else if (verificationStatus.equals("3")) {
                        itemViewHolder.mengban.setVisibility(View.VISIBLE);

                    } else {
                        itemViewHolder.mengban.setVisibility(View.GONE);

                    }*/

                } else {
                    //*活动已开始*//
                    itemViewHolder.mengban.setVisibility(View.VISIBLE);

                }
            } else {
                //*活动已结束*//
                itemViewHolder.mengban.setVisibility(View.VISIBLE);

            }
            System.out.println(i < 0);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        itemViewHolder.article_information.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*点击item跳转*/
                Intent intent = new Intent();
                intent.setClass(mContext, OrderInformationActivity.class);
                intent.putExtra("showUserLogId", mDatas.get(position).getShowUserLogId());
                mContext.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView activitys_title, activitys_time, activitys_place, activitys_cost;
        ImageView imageView;
        RelativeLayout article_information;
        View mengban;

        public ItemViewHolder(View itemView) {
            super(itemView);
            activitys_title = (TextView) itemView.findViewById(R.id.activitys_title);
            activitys_time = (TextView) itemView.findViewById(R.id.activitys_time);
            activitys_place = (TextView) itemView.findViewById(R.id.activitys_place);
            activitys_cost = (TextView) itemView.findViewById(R.id.activitys_cost);
            imageView = (ImageView) itemView.findViewById(R.id.img_msg);
            article_information = (RelativeLayout) itemView.findViewById(R.id.article_information);
            mengban = itemView.findViewById(R.id.mengban);
        }


    }

}
